/**
 * 笔记列表
 */
var localStorageManagers = require("script/localStorageManagers.js");
//mui初始化
mui.init({
	swipeBack:true
});
//plus初始化
mui.plusReady(function(){
	plusReady();
});
//plusReady
function plusReady(){
	//滚动条
	mui('.mui-scroll-wrapper').scroll({
					indicators: false //是否显示滚动条
				});
	var self=plus.webview.currentWebview();
	document.getElementById('catalogname').innerText=self.catalogname;
	//加载课题下笔记信息
	loadNoteDiv();
	//打开查阅意见
	mui('.footbtndiv').on('tap','.footbtnspan',openNoteview);
	//笔记详情
	mui('.mui-content').on('tap', '.mui-slider-handle', openNoteContent);
	//按钮
	if(self.viewed!=0)document.getElementById('nav-mui-bar').style.display='block';
	 //删除笔记
	if('Android'==plus.os.name)mui('.mui-content').on('longtap', '.mui-slider-handle', deleteNote);
	if('iOS'==plus.os.name){mui('.mui-content').on('tap', '.mui-btn-red', deleteNote);}
};
//加载笔记列表
function loadNoteDiv() {
	document.getElementById('notes').innerHTML=' ';
	ajaxLoadNote();
}
//ajax加载笔记列表
function ajaxLoadNote() {
	var userid = localStorageManagers.getUseridSession();
	var catalogid=plus.webview.currentWebview().catalogid;
	var url = serverAjax + "/mobile/note/getNoteListByCatalogid.do";
	mui.ajax(url, {
		data: {
			userid: userid,
			catalogid:catalogid
		},
		type: 'post',
		dataType: 'json',
		timeout: 10000,
		success: function(data) {
			if (data == null) return;
			buildNoteDiv(data);
		},
		error: function(e1, e2, e3) {
			ajaxErrorTip();
			console.log(e1);
		}
	});
};
//buildNoteDiv
function buildNoteDiv(data){
	var notesEle=document.getElementById('notes');
	for(var i=0;i<data.length;i++){
		var catClass=getCatClass(data[i].resCatName);
		var zaned='';
	    if(data[i].enlighten==1)zaned='zaned';
	    var delstr='';
	    var resname=data[i].resName;
	    if(resname.length>6)resname=resname.substring(0,6)+'...';
	    var notecontent=data[i].noteContent;
	    if(notecontent.length>18)notecontent=notecontent.substring(0,18)+'...';
	    if('iOS'==plus.os.name)delstr='<div class="mui-slider-right mui-disabled"><a class="mui-btn mui-btn-red">删除</a></div>';
		var notestr='<li class="mui-table-view-cell note" noteid="'+data[i].noteId+'" rescatname="'+data[i].resCatName+'" resname="'+data[i].resName+'" notecontent="'+data[i].noteContent+'">'+delstr
						+'<div class="mui-slider-handle"><div class="c1"><span class="zan '+zaned+'"></span><span class="restype-div '+catClass+'">'+data[i].resCatName+'</span><span class="resname-div">'+resname+'</span><span class="date">'+data[i].noteDate+'</span></div> '
						+'<div class="c2">'+notecontent+'</div></div>'
					+'</li>';
		notesEle.innerHTML=notesEle.innerHTML+notestr;
	}
}
function getCatClass(preName) {
	var preClass = 'xt';
	switch (preName) {
		case "视频":
			preClass = 'sp';
			break;
		case "讲义":
			preClass = 'jy';
			break;
		case "习题":
			preClass = 'xt';
			break;
		case "教案":
			preClass = 'jy';
			break;
		case "随堂检测":
			preClass = 'stjc';
			break;
		default:
			preClass = 'xt';

	}
	return preClass;
};
//打开查阅意见
function openNoteview(){
	var catalogid=plus.webview.currentWebview().catalogid;
	mui.openWindow({
		url:'noteview.html',
		id:'selfcenter/note/noteview',
		extras:{catalogid:catalogid}
	});
};
//删除笔记
function deleteNote() {
	var elem = this;
	var li = elem.parentNode;
	if('iOS'==plus.os.name)li=li.parentNode;
	mui.confirm('确认删除该条记录？', '', null, function(e) {
		if (e.index == 0) {
			var noteid = li.getAttribute('noteid');
			var url = serverAjax + "/mobile/note/delNote.do";
			mui.ajax(url,{
				data:{noteid:noteid},
				type:'post',
				dataType:'json',
				timeout:10000,
				success:function(data){
					if (data == null) return;
					li.parentNode.removeChild(li);
				},
				error:function(e1,e2,e3){
					
				}
			});
			
		} else {
			setTimeout(function() {
				mui.swipeoutClose(li);
			}, 0);
		}
	});
};
//笔记详情
function openNoteContent(){
	var node=this.parentNode;
   var noteid = node.getAttribute('noteid');
   var rescatname = node.getAttribute('rescatname');
   var resname = node.getAttribute('resname');
   var notecontent= node.getAttribute('notecontent');
   mui.openWindow({
		url: 'noteContent.html',
		id: 'selfcenter/note/noteContent',
		extras: {
			noteid: noteid,
			rescatname:rescatname,
			resname:resname,
			notecontent:notecontent
		}
	});
}
